package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.apache.commons.codec.binary.Base64;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/system/test")
public class SysTestController extends BaseController {
    @PostMapping("/file")
    public AjaxResult export(@RequestParam("file") MultipartFile file) {
        String validJpegBase64 = convertMultipartFileToBase64(file);
        return AjaxResult.success(isValidImageBase64(validJpegBase64));
    }

    public boolean isValidImageBase64(String base64Image) {
        base64Image = base64Image.substring(0, 3);
        return "/9j".equals(base64Image) || "iVB".equals(base64Image);
    }

    public static String convertMultipartFileToBase64(MultipartFile file) {
        try (InputStream inputStream = file.getInputStream()) {
            byte[] bytes = new byte[(int) file.getSize()];
            inputStream.read(bytes);
            return Base64.encodeBase64String(bytes);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
